﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Billing
{
    public class FormatManager
    {
        public FormatManager(string FormatText)
        {
            this.Execute(FormatText);
        }
        public KeyValueList ParameterList = new KeyValueList();


        int Level = 0;

        private string FormatWriter(string str)
        {
            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"\{@(?<Name>\w*)\}");
            int MatchCount = re.Matches(str).Count;
            if (MatchCount > 0)
            {
                System.Text.RegularExpressions.GroupCollection grs = re.Matches(str)[0].Groups;

                ParameterList.Add(grs["Name"].Value, string.Empty);
                str = str.Replace("@" + grs["Name"].Value, Level.ToString());
            }
            Level++;
            if (MatchCount > 1)
                str = FormatWriter(str);

            return str;
        }
        private string Format = string.Empty;
        private void Execute(string FormatText)
        {
            Level = 0;
            ParameterList.Clear();
            Format = FormatWriter(FormatText);
        }
        public string GetText()
        {
            string[] objs = new string[ParameterList.Count];
            for (int i = 0; i < ParameterList.Count; i++)
            {
                objs.SetValue(ParameterList[i].Value, i);
            }

            return string.Format(Format, objs);
        }
    }
    [Serializable]
    public class KeyValue
    {
        public KeyValue()
        {
        }
        public KeyValue(string Key, string Value)
        {
            this.mKey = Key;
            this.mValue = Value;
        }
        private string mKey;
        [System.Xml.Serialization.XmlAttribute]
        public string Key
        {
            get { return mKey; }
            set { mKey = value; }
        }

        private string mValue;
        public string Value
        {
            get { return mValue; }
            set { mValue = value; }
        }
    }
    [Serializable]
    public class KeyValueList : System.ComponentModel.BindingList<KeyValue>
    {
        SortedList<string, KeyValue> objs = new SortedList<string, KeyValue>();
        public System.Collections.ArrayList Values = new System.Collections.ArrayList();

        protected override void RemoveItem(int index)
        {
            objs.Remove(base[index].Key);
            base.RemoveItem(index);
        }

        protected override void ClearItems()
        {
            base.ClearItems();
            objs.Clear();
        }

        protected override void InsertItem(int index, KeyValue item)
        {
            this.Values.Add(item.Value);
            base.InsertItem(index, item);
            objs.Add(item.Key, item);

        }

        public string this[string Key]
        {
            get { return objs[Key].Value; }
            set { objs[Key].Value = value; }
        }

        public bool ContainsKey(string key)
        {
            return this.objs.ContainsKey(key);
        }

        public void Add(string Key, string Value)
        {
            base.Add(new KeyValue(Key, Value));
        }

    }
}
